package com.travel.carmen;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;


/**
	Voice Recognition best matches selection class for the Travel Mobile Application Carmen.
	 The View is the Matches List View (layout as per recorded_matches.xml) for the application.
	Users select from a list of best matching results for voice input
	@author Arturo Aparicio 
	@author Edwin Boykin 
	@author Veena Chengalnaidu
	@author Anton Ganeshalingam
	@author Lakshmi Mallampati
	@author Tarun Morton
	@author Chantha Thoeun
*/
public class VoiceRecognized extends Activity implements OnItemClickListener 
{
	private ListView matches_list;
	private ArrayList<String> matches;
	private String result = "";
	
	@Override
	/**
	   Called when the activity is first created.
	    The UI activities as per recoded_matches.xml are inflated.
	    All listeners and adapters are attached and all settings are
	    made to default.
	 */
	public void onCreate(Bundle bundle)
	{
		super.onCreate(bundle);
		setContentView(R.layout.recorded_matches);
		Log.d("VR", "set view");
		matches = getIntent().getStringArrayListExtra(getString(R.string.query_best_match));
		Log.d("VR", matches.get(0));
		matches_list = (ListView) findViewById(R.id.list_of_matches);		
		matches_list.setAdapter(new ArrayAdapter<String>(this, 
    			android.R.layout.simple_list_item_1, matches));
    	matches_list.setOnItemClickListener(this);
	}
	
	
	/**
	   List View Listener for voice input best matches by Android Voice Recognition.
	    Returns the selected result and control back to the Main interface of Carmen
	    Travel Mobile Application.
	   @param arg0 Adapter for this listener
	   @param arg1 View for this listener
	   @param arg2 Index of selected Match for voice input
	   @param arg3 --
	 */
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		result = matches_list.getAdapter().getItem(arg2).toString();
		Intent resultIntent = new Intent();
		resultIntent.putExtra(getString(R.string.query_best_match), result);
		setResult(Activity.RESULT_OK, resultIntent);
		finish();
	}
}
